package lp.xian.materials.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MathUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.materials.domain.*;
import lp.xian.materials.mapper.PurchaseTableMapper;
import lp.xian.materials.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.materials.mapper.PurchaseMapper;
import lp.xian.materials.service.IPurchaseService;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 采购订单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-06
 */
@Service
public class PurchaseServiceImpl implements IPurchaseService
{
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private WarehousingRequisitionServiceImpl warehousingRequisitionService;
    @Autowired
    private WarehousingRequisitionTableServiceImpl warehousingRequisitionTableService;

    /**
     * 查询采购订单
     *
     * @param purchaseId 采购订单主键
     * @return 采购订单
     */
    @Override
    public Purchase selectPurchaseByPurchaseId(Integer purchaseId)
    {
        return purchaseMapper.selectPurchaseByPurchaseId(purchaseId);
    }

    /**
     * 查询采购订单列表
     *
     * @param purchase 采购订单
     * @return 采购订单
     */
    @Override
    public List<Purchase> selectPurchaseList(Purchase purchase)
    {
        return purchaseMapper.selectPurchaseList(purchase);
    }

    /**
     * 新增采购订单
     *
     * @param purchase 采购订单
     * @return 结果
     */
    @Override
    public int insertPurchase(Purchase purchase)
    {
        purchase.setCreateTime(DateUtils.getNowDate());
        return purchaseMapper.insertPurchase(purchase);
    }

    /**
     * 修改采购订单
     *
     * @param purchase 采购订单
     * @return 结果
     */
    @Override
    public int updatePurchase(Purchase purchase)
    {
        purchase.setUpdateTime(DateUtils.getNowDate());
        return purchaseMapper.updatePurchase(purchase);
    }

    /**
     * 批量删除采购订单
     *
     * @param purchaseIds 需要删除的采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseByPurchaseIds(Integer[] purchaseIds)
    {
        return purchaseMapper.deletePurchaseByPurchaseIds(purchaseIds);
    }

    /**
     * 删除采购订单信息
     *
     * @param purchaseId 采购订单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseByPurchaseId(Integer purchaseId)
    {
        return purchaseMapper.deletePurchaseByPurchaseId(purchaseId);
    }

    @Override
    public int auditPurchase(PurchaseVo purchaseVo) /*throws ParseException*/{
        List<PurchaseTable> purchaseTables = purchaseVo.getOrderList();
        Purchase purchase=new Purchase();
        for (int i = 0; i < purchaseTables.toArray().length; i++) {
            purchase.setPurchaseId(purchaseVo.getPurchaseId());
            purchase.setAuditStatus(1);
            purchase.setAuditTime(new Date());
            purchase.setAuditPerson(SecurityUtils.getUsername());
        }
        purchaseMapper.updatePurchase(purchase);

        if(purchase.getAuditStatus().intValue()==1) {
            WarehousingRequisition warehousingRequisition=new WarehousingRequisition();
            String orderNo=createNo(purchaseVo.getPurchaseId().toString());
            warehousingRequisition.setWarehousingRequisitionOrder(orderNo);
            warehousingRequisition.setSupplierId(purchaseVo.getSupplierIds());//供应商
            warehousingRequisition.setWarehousingRequisitionPerson(purchaseVo.getPurchasePerson());//业务员
            warehousingRequisition.setAuditStatus(0);//审核状态
            warehousingRequisition.setRequisitionType(purchaseVo.getRequisitionType());//采购类型
            warehousingRequisition.setWarehousingDate(purchaseVo.getPurchaseDate());//日期
            warehousingRequisition.setCreateBy(SecurityUtils.getUsername());
            warehousingRequisition.setCreateTime(new Date());
            warehousingRequisitionService.insertWarehousingRequisition(warehousingRequisition);

            WarehousingRequisitionTable warehousingRequisitionTable = new WarehousingRequisitionTable();
            for (int k = 0; k < purchaseTables.toArray().length; k++) {
                warehousingRequisitionTable.setWarehousingRequisitionId(warehousingRequisition.getWarehousingRequisitionId());
                warehousingRequisitionTable.setWarehousingOrderNo(purchaseTables.get(k).getPurchaseOrderNo());
                warehousingRequisitionTable.setWarehousingRequisitionTableName(purchaseTables.get(k).getPurchaseTableName());
                warehousingRequisitionTable.setWarehousingRequisitionTableSpeion(purchaseTables.get(k).getPurchaseTableSpeion());//规格型号
                warehousingRequisitionTable.setWarehousingRequisitionTableNum(purchaseTables.get(k).getPurchaseTableNum());
                warehousingRequisitionTable.setWarehousingRequisitionTableUnit(purchaseTables.get(k).getPurchaseTableUnit());//主计量单位
                warehousingRequisitionTable.setWarehousingRequisitionTablePrice(purchaseTables.get(k).getPurchasePrice());//单价
                warehousingRequisitionTable.setWarehousingRequisitionTotalPrice(purchaseTables.get(k).getPurchaseTotalPrice());//总价
                warehousingRequisitionTable.setWarehousingRequisitionTaxPrice(purchaseTables.get(k).getPurchaseTaxPrice());//税额
                warehousingRequisitionTable.setWarehousingRequisitionUnitTotalPrice(purchaseTables.get(k).getPurchaseUnitTotalPrice());//含税总金额
                warehousingRequisitionTableService.insertWarehousingRequisitionTable(warehousingRequisitionTable);
            }
        }
        return 1;
    }
    public String createNo(String id)/*throws ParseException */{
        String no="";
        StringBuffer buffer=new StringBuffer(02);
        buffer.append(MathUtils.random());
        if (id.length()<3){
            for(int i=3-id.length();i>0;i--){
                buffer.append("0");
            }
        }
        no=buffer.append(id).toString();
        Map<String,Object> params=new HashMap<String, Object>();
        return no;
    }
}
